package com.aasa.siccosap.dao;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import oracle.jdbc.OracleTypes;

import com.aasa.siccosap.bean.ObraBean;
import com.aasa.siccosap.bean.ValorCombo;
import org.apache.log4j.Logger;
import com.aasa.siccosap.bean.ParametrosGenerales;
import com.aasa.siccosap.util.Constantes;
import javax.sql.DataSource;

public class AuxiliarDAO extends accesoDao{
	
	static Logger logger = Logger.getLogger("AuxiliarDAO.class");
	public AuxiliarDAO()	throws accesoDaoException {
		super();
	}
	
	public AuxiliarDAO(DataSource dataSource) {
		super(dataSource);
	}
	
	
	public List<ValorCombo> obtenerValoresObrasCliente(ParametrosGenerales parametros) throws Exception{
        ResultSet objL_rs = null;
        this.av_message = null;
        this.storedProcedure = "begin SICCOSPK_PROYECTO.SICCOSSP_OBRAS_CLIENTE(?,?,?); end;";
        List<ValorCombo> aobjL_consultaBloqueosObra=new ArrayList<ValorCombo>();
        try{
                this.conn = dataSource.getConnection();
                this.stmt = this.conn.prepareCall(storedProcedure);
                configuraParametrosConsultaObras(parametros);
                this.stmt.execute();
                this.av_message = (String)stmt.getObject(1);
                if(this.av_message.trim().toUpperCase().endsWith(Constantes.NO_ERRORS)){
                	objL_rs = (ResultSet)stmt.getObject(2);
                        while(objL_rs.next()){
                        		ValorCombo detalleObra = creaRegistroConsultaObras(objL_rs);
                        		aobjL_consultaBloqueosObra.add(detalleObra);
                        }
                }else{
                    return null;
                }
                
                
        }catch (Exception e){
                e.printStackTrace();
                return null;
        }finally{
        	close(this.conn, objL_rs, stmt);
        }
        return(aobjL_consultaBloqueosObra);
    }

	private ValorCombo creaRegistroConsultaObras(ResultSet rs)throws SQLException {
		ValorCombo bean = new ValorCombo();
		
		bean.setStrPb_codigo(rs.getString("NUM_OBRA"));
		bean.setStrPb_valor(rs.getString("NUM_OBRA")+" - "+rs.getString("DESC_OBRA"));
		        
        return bean;
    	
	}

	private CallableStatement configuraParametrosConsultaObras(ParametrosGenerales parametros) throws SQLException{
		stmt.registerOutParameter(1, OracleTypes.VARCHAR);               
        stmt.registerOutParameter(2, OracleTypes.CURSOR);
        stmt.setString(3, parametros.getStrPb_codigoCliente());
		return stmt;		
	}
}
